<?php require "db.php"; ?>
<?php

    $save = false;

    if($_GET['id'] && $_GET['action'] == 'remove'){
        $ids = $_GET['id'];
        foreach($ids as $id){
            $query = mysqli_query($con, "DELETE FROM architects WHERE id=".$id);
        }
        header('Location: architects.php');
    }

    if($_POST['save']){
        $name = $_POST['name'];
        $price = $_POST['price'];
        $info = $_POST['info'];

        if($name && $price && $info){
            if($_GET['id']){
                $query = mysqli_query($con, "UPDATE architects SET name='".$name."', info='".$info."', price=".$price." WHERE id=".$_GET['id'][0]);
                $save = true;
            }else{
                $query = mysqli_query($con, "INSERT INTO architects (name, info, price) VALUES ('".$name."', '".$info."', $price)");
                $save = true;
            }
        }
    }
    if($_GET['id']){
        $architect = mysqli_query($con, "SELECT * FROM architects WHERE id=".$_GET['id'][0]);
        $data = mysqli_fetch_assoc($architect);
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Admin Panel - Projects</title>
    <link href="css/bootstrap-responsive.css" rel="stylesheet" type="text/css" />
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
    <header>
        <h3 class="muted">Admin Panel</h3>
        <div class="navbar">
            <div class="navbar-inner">
                <div class="container">
                    <ul class="nav">
                        <li>
                            <a href="index.php">Home</a>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Projects <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li><a href="projects.php">View</a></li>
                                <li><a href="project.php">Add Project</a></li>
                            </ul>
                        </li>
                        <li class="dropdown active">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Architects <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li><a href="architects.php">View</a></li>
                                <li><a href="architect.php">Add Architect</a></li>
                            </ul>
                        </li>
                        <li>
                            <a href="#">Settings</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </header>
    <section>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li><a href="architects.php">Architects</a> <span class="divider">/</span></li>
            <li class="active">Add Architect</li>
        </ul>
        <form action="" method="POST">
        <div class="row-fluid">
            <div class="span8">
                <?php if($save){ ?>
                    <div class="alert alert-success"><strong>Well done!</strong> Item was saved successfully.</div>
                <?php }elseif($_POST['save']){ ?>
                    <div class="alert alert-error"><strong>Uppsss!</strong> Check out all field and fill them.</div>
                <?php } ?>
            </div>
            <div class="span4 text-right">
                <button type="submit" name="save" value="1" class="btn btn-primary">Save</button>
                <button type="reset" class="btn btn-warning">Clear</button>
            </div>
        </div>
        <hr />
        <div>
            <div class="control-group <?php if($_POST['save'] && !$name) echo "error"; ?>">
                <label class="control-label" for="inputName">Name</label>
                <div class="controls">
                    <input type="text" name="name" value="<?php echo $data['name']; ?>" class="input-xlarge" id="inputName" placeholder="Architect Name">
                    <?php if($_POST['save'] && !$name) { ?>
                        <span class="help-inline">Please correct the error</span>
                    <?php } ?>
                </div>
            </div>
            <div class="control-group <?php if($_POST['save'] && !$info) echo "error"; ?>">
                <label class="control-label" for="inputName">Info</label>
                <div class="controls">
                    <textarea class="input-xlarge" name="info" rows="5" placeholder="Architect Info"><?php echo $data['info'];?></textarea>
                    <?php if($_POST['save'] && !$info) { ?>
                        <span class="help-inline">Please correct the error</span>
                    <?php } ?>
                </div>
            </div>
            <div class="control-group <?php if($_POST['save'] && !$price) echo "error"; ?>">
                <label class="control-label" for="inputCost">Price</label>
                <div class="controls">
                    <div class="input-prepend input-append">
                        <span class="add-on">$</span>
                        <input class="span2" id="inputCost" value="<?php echo $data['price']; ?>" name="price" type="text">
                        <span class="add-on">.00 per hour</span>
                    </div>
                    <?php if($_POST['save'] && !$price) {?>
                        <span class="help-inline">Please correct the error</span>
                    <?php } ?>
                </div>
            </div>
        </div>
        <div class="text-right">
            <hr />
            <button type="submit" name="save" value="1" class="btn btn-primary">Save</button>
            <button type="reset" class="btn btn-warning">Clear</button>
        </div>
        </form>
    </section>
    <footer>
        <hr />
        <p>&copy; Company 2013</p>
    </footer>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/default.js"></script>
</body>
</html>